<!--
 * @Author: White
 * @description: 网易云音乐
 * @LastEditTime: 2021-10-20 22:55:51
-->

<template>
    <div class="music">
        <div class="content">
            <!-- 顶部 -->
            <div class="content-header" :style="{ 'background': theme.color }">
                <div class="content-header-options" :style="{ 'background': theme.color }">
                    <div class="content-header-circle">
                        <span class="red"></span>
                        <span class="yellow"></span>
                        <span class="green"></span>
                    </div>
                    <div class="back-next">
                        <i class="el-icon-arrow-left" @click="back"></i>
                        <i class="el-icon-arrow-right" @click="next" style="padding-left: 10px"></i>
                    </div>
                </div>
                <div class="content-header-nav">
                        <ul>
                            <!-- 导航栏 -->
                            <li v-for="(item, index) in nav" :key="index" 
                                :class="[url === item.href && 'active']"
                                @click="navhref(item.href)"
                                :style="{ 'color': theme.text }">
                                <span>{{item.name}}</span>
                            </li>
                            <!-- 搜索 -->
                            <li class="search">
                                <input id="search-input" type="text" v-model="parmas.search" placeholder="搜索" @keyup.enter="InputSearch" @focus="handleFocus">
                                <div class="search-list" v-if="SearchListShow">
                                    <SearchList/>
                                </div>
                            </li>
                            <!-- 换肤 -->
                            <li class="changeColor">
                                <ChangeColor/>
                            </li>
                        </ul>
                </div>
            </div>
            <!-- 主体 -->
            <div class="content-main">
                <div class="content-main-left" :style="{ 'background': theme.color }">
                    <!-- 登录模块 -->
                    <Login @register="register"/>
                    <!-- 收藏歌单 -->
                    <CollectedPlayList />
                </div>
                <div class="content-main-right" :style="{ 'background': theme.Mcolor }">
                    <router-view></router-view>
                </div>
            </div>
            <!-- 当前播放歌曲信息 -->
            <transition name="show">
                <div class="song-info" v-show="songInfoDialog">
                    <SongInfo/>
                </div>
            </transition>
            <!-- 歌曲播放进度 -->
            <div class="player">
                <Player/>
            </div>
            <!-- 登录输入框 -->
            <div class="login-dialog" v-if="isLogin">
                <div class="login-header">
                    <i class="el-icon-close" @click="isLogin = false"></i>
                </div>
                <div class="login-content">
                    <div class="login-bg">
                        <img src="../assets/images/loginbg.png" alt="">
                    </div>
                    <el-form :model="loginForm" status-icon :rules="rules" label-width="20%" label-position="center" class="demo-ruleForm">
                        <el-form-item label="手机号" prop="phone">
                            <el-input type="text" v-model="loginForm.phone"></el-input>
                        </el-form-item>
                        <el-form-item label="密码" prop="password">
                            <el-input type="password" v-model="loginForm.password"></el-input>
                        </el-form-item>
                        <div class="btn" @click="submitForm(loginForm)">登录</div>
                    </el-form>
                </div>
            </div>
        </div>
  </div>
</template>

<script>
import { useStore } from 'vuex'
import { useRouter, useRoute } from 'vue-router'
import { ref, reactive, onMounted, computed, toRefs } from 'vue'
import Banner from '@/components/MusicView/banner'
import PrivateContent from '@/components/MusicView/PrivateContent'
import NewMusic from '@/components/MusicView/NewMusic'
import RecomMv from '@/components/MusicView/RecomMv'
import AnchorRadio from '@/components/MusicView/AnchorRadio'
import ChangeColor from '@/components/MusicView/ChangeColor'
import SongInfo from '@/components/MusicView/SongInfo'
import SearchList from '@/components/MusicView/SearchList'
// 左侧组件
import Player from '@/components/MusicView/Player'
import Login from '@/components/MusicView/Login'
import CollectedPlayList from '@/components/MusicView/CollectPlaylist'

import { getLogin } from '@/api/music'
import { validateVIphone } from "@/utils/loginRules"
import { getSetToken } from '@/utils/localstrong' // 按需导入要使用的方法

import { ElNotification } from 'element-plus'
import { logs } from "@/message/Log"

export default ({
  components: {
    Banner,
    PrivateContent,
    NewMusic,
    RecomMv,
    AnchorRadio,
    Player,
    Login,
    CollectedPlayList,
    ChangeColor,
    SongInfo,
    SearchList
  },
  setup () {
    const store = useStore()
    const router = useRouter()
    onMounted(()=> {
        // 更新通知
        const log = logs[0]
        ElNotification({
            title: log.title,
            customClass: "custom-notification",
            dangerouslyUseHTMLString: true,
            message: log.briefContent,
            duration: 10000,
            onClose: () => ElNotification.closeAll()
        })

        // 点击事件
        const searchInputEle = document.querySelector("#search-input");
        document.addEventListener("click", function (e) {
            /** 被点击的元素不是searchInputEle，就隐藏searchInputEle */
            if (e.target !== searchInputEle) {
                SearchListShow.value = false
            }
        })
    })
    const songInfoDialog = computed(() => store.state.songInfoDialog)
    // 当前颜色
    const theme = computed(() => store.state.theme)
    // 当前路由
    const AtRouter = reactive({
        url: useRoute().path
    })
    // 登录弹窗
    const isLogin = ref(false)
    // 搜索推荐列表
    const SearchListShow = ref(false)
    // 表单绑定数据
    const loginForm = reactive({
        phone: '',
        password: ''
    })
    const navList = reactive({
        nav: [
            { name: '个性推荐', href: '/music' },
            { name: '歌单', href: '/music/songlist' },
            { name: '排行榜', href: '/music/rank' },
            { name: '歌手', href: '/music/singer' },
            { name: '最新音乐', href: '/music/newmusic' }
        ]
    })
    // 搜索参数
    const parmas = reactive({
        search: ''    
    })
    // 邮箱验证
    let validateIphone = (rule, value, callback) => {
      if (value == "") callback(new Error("账号(手机)不能为空！"));
      else if (validateVIphone(value)) {
        callback(new Error("手机格式有误！"));
      } else {
        callback();
      }
    };
    // 密码验证
    let validatePassword = (rule, value, callback) => {
      if (value == "") callback(new Error("密码不能为空！"))
      else callback()
    }
    // 表单验证
    const rules = reactive({
      phone: [
        { validator: validateIphone, trigger: "blur" }
      ],
      password: [
        { validator: validatePassword, trigger: "blur" }
      ]
    })
    // 子组件触发的弹窗
    const register = (val) => {
        isLogin.value = val
    }
    const back = () => {
        router.back()
    }
    const next = () => {
        router.go(1)
    }
    // 展开搜索推荐
    const handleFocus = () => {
        SearchListShow.value = true
    };
    // 搜索
    const InputSearch = async () => {
        router.push({
            path: '/music/search',
            query: { keywords: parmas.search }
        })
    }
    const navhref = (href) => {
        AtRouter.url = href
        router.push({
            path: href,
        })
    }
    // 提交登录
    const submitForm = async (val) => {
        const res = await getLogin(val)
        if (res.data.code === 200) {
            isLogin.value = false
            // 登录信息存本地
            getSetToken('userInfo', res.data)
            store.commit('userInfo', res.data)
        } else {
           alert('报错了') 
        }
    }
    return {
        ...toRefs(navList),
        ...toRefs(AtRouter),
        theme,
        AtRouter,
        loginForm,
        store,
        rules,
        isLogin,
        SearchListShow,
        parmas,
        register,
        back,
        next,
        navhref,
        InputSearch,
        handleFocus,
        submitForm,
        songInfoDialog
    }
  }
})
</script>

<style lang="scss" scoped>
@keyframes hide {
    0% {
        opacity: 1;
        left: 0;
    }
    100% {
        opacity: 0;
        bottom: -100px;
    }
}
.search-list {
    height: calc(100vh - 550px);
    position: absolute;
    z-index: 999;
}
.show-enter-active {
    animation: show 1s;
}
.show-leave-active {
    animation: hide 1s;
}
.show-enter, .show-leave-to {
    opacity: 0;
}

.show-enter-to {
    opacity: 1;
}

.music {
    max-width: 1200px;
    height: calc(100vh - 77px);
    margin: 0 auto;
    position: relative;
    .content {
        width: 100%;
        border-radius: 10px;
        position: relative;
        left: 0;
        right: 0;
        top: 40px;
        margin: 0 auto;
        box-shadow: 20px 0 20px 2px #e6e6e6, -20px 0 20px 2px #e6e6e6, 0 20px 20px 2px #e6e6e6;
        overflow: hidden;
        .content-header {
            height: 50px;
            display: flex;
            align-items: center;
            border-radius: 10px 10px 0px 0px;
            .content-header-options {
                width: 200px;
                height: 50px;
                padding: 0px 20px;
                box-sizing: border-box;
                display: flex;
                align-items: center;
                justify-content: space-between;
                .content-header-circle {
                    z-index: 999;
                    span {
                        margin: 0px 4px;
                    }
                    .red {
                        width: 10px;
                        height: 10px;
                        display: inline-block;
                        border-radius: 50%;
                        background-color: rgb(255, 92, 92);
                    }
                    .yellow {
                        width: 10px;
                        height: 10px;
                        display: inline-block;
                        border-radius: 50%;
                        background-color: rgb(255, 187, 56);
                    }
                    .green {
                        width: 10px;
                        height: 10px;
                        display: inline-block;
                        border-radius: 50%;
                        background-color: rgb(41, 202, 56);
                    }
                }
                .back-next {
                    i {
                        cursor: pointer;
                    }
                }
            }
            .content-header-nav {
                width: 1000px;
                ul {
                    display: flex;
                    align-items: center;
                    li:first-child {
                        margin-left: 20px;
                    }
                    li {
                        margin-right: 20px;
                        font-size: 14px;
                        font-weight: 300;
                        cursor: pointer;
                    }
                    .active {
                        font-weight: bolder;
                        color: #000;
                    }
                    .search {
                        margin-left: 20px;
                        position: relative;
                        input {
                            width: 140px;
                            height: 26px;
                            line-height: 26px;
                            border: none;
                            outline:none;
                            border-radius: 13px;
                            background-color: #ececec;
                            padding-left: 26px;
                            background-image: url('../assets/images/搜索.png');
                            background-repeat: no-repeat;
                            background-size: 12px 12px;
                            background-position: 8px 8px;
                            font-size: 10px;
                            font-weight: 300;
                        }
                    }
                }
            }
        }
        .content-main {
            width: 100%;
            height: calc(70vh - 32px);
            display: flex;
            .content-main-left {
                width: 200px;
                height: 100%;
                background-color: #eaeaea;
            }
            .content-main-right::-webkit-scrollbar {
                width: 8px;
                height: 8px;
            }
            .content-main-right::-webkit-scrollbar {
                -webkit-box-shadow: inset 0 0 5px rgba(146, 145, 145, 0.1);
            }
            .content-main-right::-webkit-scrollbar-thumb {
                border-radius: 10px;
                -webkit-box-shadow: inset 0 0 5px rgba(146, 145, 145, 0.1);
                background: rgba(138, 137, 137, 0.2);
            }
            .content-main-right {
                width: 1000px;
                height: auto;
                background-color: #ffffff;
                padding: 0px 20px;
                box-sizing: border-box;
                overflow: scroll;
                overflow-x: hidden;
                .banner {
                    margin: 10px 0px;
                }
                .recommend {
                    .title {
                        height: 30px;
                        display: flex;
                        align-items: center;
                        h3 {
                            line-height: 30px;
                            font-size: 16px;
                            color: #000;
                            font-weight: 700;
                            text-decoration: none
                        }
                    }
                    .recommend-wrap {
                        display: flex;
                        justify-content: space-between;
                        flex-wrap: wrap;
                        .recommend-item:nth-child(5) {
                            margin-right: 0px;
                        }
                        .recommend-item:nth-child(10) {
                            margin-right: 0px;
                        }
                        .recommend-item {
                            width: 170px;
                            height: 170px;
                            margin-right: 10px;
                            margin-bottom: 50px;
                            border-radius: 4px;
                            position: relative;
                            cursor: pointer;
                            img {
                                width: 170px;
                                height: 170px;
                                cursor: pointer;
                                border-radius: 4px;
                            }
                            p {
                                font-size: 14px;
                                font-weight: 400;
                                line-height: 20px;
                                overflow: hidden;
                                text-overflow: ellipsis;
                            }
                            .play {
                                font-size: 14px;
                                font-weight: 300;
                                line-height: 20px;
                                position: absolute;
                                top: 10px;
                                right: 10px;
                                color: #fff;
                            }
                        }
                    }
                }
            }
        }
        // 当前歌曲信息
        .song-info {
            width: 100%;
            height: calc(100% - 60px);
            padding: 30px 30px;
            box-sizing: border-box;
            position: absolute;
            overflow-x: hidden;
            overflow-y: scroll;
            left: 0;
            right: 0;
            top: 0px;
            background-color: #fff;
            border-radius: 10px 10px 0px 0px;
            z-index: 99;
        }
        .song-info::-webkit-scrollbar {/*隐藏滚轮*/
            display: none;
        }
        // 登录弹窗
        .login-dialog {
            width: 350px;
            height: 410px;
            padding: 10px 20px;
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            margin: auto;
            z-index: 200;
            box-sizing: border-box;
            border-radius: 10px;
            // background: rgba(255, 255, 255, 0.504);
            background-color: #fff;
            box-shadow: 0 18px 32px 0 rgba( 31, 38, 135, 0.37 );
            backdrop-filter: blur( 19.0px );
            -webkit-backdrop-filter: blur( 19.0px );
            border-radius: 10px;
            transform: scale(1);
            .login-header {
                width: 100%;
                height: 30px;
                display: flex;
                justify-content: flex-start;
                align-items: center;
            }
            .login-content {
                padding: 0px 20px;
                .login-bg {
                    margin-bottom: 20px;
                    img {
                        width: 100%;
                        height: 100px;
                        opacity: .5;
                        // -webkit-filter: blur(1px);
                    }
                }
                :deep(.el-form-item) {
                    margin-bottom: 20px;
                    input {
                        height: 30px;
                    }
                }
                :deep(.el-form-item__label) {
                    font-size: 10px;
                }
            }
            .login-content {
                text-align: center;
                .btn {
                    margin-top: 10px;
                    width: 60%;
                    height: 30px;
                    line-height: 30px;
                    background-color: #cd2f2d;
                    border-radius: 6px;
                    border: none;
                    color: #fff;
                    text-align: center;
                    margin: 0 auto;
                }
            }
        }
    }
}
</style>
